package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.PrefStrategyBean;
import com.koron.css2.baseConfig.bean.PrefStrategyDetailBean;
import com.koron.css2.baseConfig.mapper.PrefStrategyMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.ValidationKey;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 优惠策略-编辑
 *
 * @author 廖大富
 */
public class PrefStrategyUpdate implements ServerInterface {

    private static Logger logger = LogManager.getLogger(PrefStrategyUpdate.class);

    @Override
    @ValidationKey(clazz = PrefStrategyBean.class,method = "update")
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PrefStrategyMapper mapper = factory.getMapper(PrefStrategyMapper.class);
            PrefStrategyBean bean = JsonUtils.objectToPojo(req.getData(), PrefStrategyBean.class);
            // 校验字段重复
            if (mapper.check2("strategy_name", bean.getStrategyName(), bean.getId()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "优惠策略名称：" + bean.getStrategyName() + "的信息已存在。",
                        void.class);
            }
            if (StringUtils.isNullOrEmpty(bean.getId())) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "id为空", void.class);
            }
            bean.setUpdateInfo(userInfo);
            // 删除修改前的明细数据
            mapper.delPrefStrategyDetail(bean.getId());
            // 前端传过来的修改之后的数据
            List<PrefStrategyDetailBean> prefStrategyList = bean.getPrefStrategyList();
            if (null == prefStrategyList || prefStrategyList.size() == 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "优惠明细内容不能为空！", void.class);
            }
            // 判断费用名称是否重复
            Set<String> set = new HashSet<>();
            for (PrefStrategyDetailBean ladderBean : prefStrategyList) {
                if (ladderBean.getCostId() == null || ladderBean.getMaxRange() == null || ladderBean.getMinRange() == null || ladderBean.getPrefValue() == null || ladderBean.getPrefWay() == null) {
                    return MessageBean.create(Constant.ILLEGAL_PARAMETER, "优惠明细内容不能为空！", void.class);
                }
                if (!set.add(ladderBean.getCostId())) {
                    return MessageBean.create(Constant.ILLEGAL_PARAMETER,"存在重复的费用名称。", void.class);
                }
                ladderBean.setCreateInfo(userInfo);
                ladderBean.setPrefStrategyId(bean.getId());
            }
            mapper.insertPrefStrategyDetailList(prefStrategyList);// 批量添加列表的数据
            mapper.updatePrefStrategy(bean); // 修改主表信息
        } catch (Exception e) {
            factory.close(false);
            logger.error("优惠策略修改失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "优惠策略修改失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }
}